import {GameDataArrayToObject, GameDataObjectFinalized} from "../redux/game";

class GameListHandler {

    constructor({matches, teams, events, stages,}) {
        this.matches = matches
        this.teams = teams
        this.events = events
        this.stages = stages
    }

    getMatches (key) {
        const matchesAll = this.matches
        const matchesSchedule = matchesAll.filter(match => match[2] === 1)
        const matchesLive = matchesAll.filter(match => match[2] > 1 && match[2] < 8)
        const matchesEnd = matchesAll.filter(match => match[2] === 8)
        const matchesCancel = matchesAll.filter(match => match[2] > 8)
        const m = {
            all: matchesAll,
            schedule: matchesSchedule,
            live: matchesLive,
            end: matchesEnd,
            cancel: matchesCancel,
        }
        return m[key]
    }

    listAll() {
        return this.formatData(this.matches)
    }

    listSchedule () {
        const matches = this.getMatches('schedule')
        return this.formatData(matches)
    }

    listLive() {
        const matches = this.getMatches('live')
        return this.formatData(matches)
    }

    listEnd() {
        const matches = this.getMatches('end')
        return this.formatData(matches)
    }

    listCancel() {
        const matches = this.getMatches('cancel')
        return this.formatData(matches)
    }

    formatData(matches) {

        const list = matches.map(match => {
            const gameObjInit = GameDataArrayToObject(match)
            return GameDataObjectFinalized(gameObjInit, this.teams, this.events, this.stages)
        })

        return list
    }

}

export default GameListHandler
